package br.com.me.ceap.up;


import br.com.me.ceap.BaseMojo;
import br.com.me.ceap.JarBundler;
import br.com.me.ceap.MECEAPUtils;
import org.apache.commons.io.filefilter.IOFileFilter;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

import java.io.File;

/**
 * Goal which uploads mapping files (in src/main/mapping).
 *
 * @goal uploadMappingFiles
 * @requiresProject
 */
public class UploadMappingFilesMojo extends BaseMojo {
    /**
     * Should we clean the customRoot (delete everything!) before uploading the mapping files?.
     *
     * @parameter default-value="${meceap.cleanCustomRoot}"
     * @required
     */
    protected Boolean cleanCustomRoot;
// ------------------------ INTERFACE METHODS ------------------------


// --------------------- Interface Mojo ---------------------

    @Override
    public void execute() throws MojoExecutionException, MojoFailureException {
        JarBundler jarBundler = bundleFilesInJAR("XML object/document mapping", "src/main/mapping", new File(projectBaseDir, "src/main/mapping"), new IOFileFilter() {
            @Override
            public boolean accept(File file) {
                return true;
            }

            @Override
            public boolean accept(final File dir, final String name) {
                return name.endsWith(".dom.xml") || name.endsWith(".ddm.xml") || name.endsWith(".dsm.xml")
                        || name.endsWith(".dfm.xml") || name.endsWith(".gpm.xml") || name.endsWith(".screen.xml")
                        || name.endsWith(".tmm.xml");
            }
        },
        true);
        WebClient webClient = MECEAPUtils.forUploadMappingFilesJAR(this, cleanCustomRoot);
        uploadFileToWebClient(webClient, jarBundler.toByteArray(), "XML object/document mapping");
    }
}
